package cn.handong.tool.沪房;

import org.junit.Test;

public class 个税计算器 {

    Double rentDeduction = 1500d;//每月房租抵税额
    Double insurancesFund = 4725d;//公积金社保抵扣
    Double startPoint = 5000d;//起征点

    Double mergeTax = 0d;    //合并计税税额
    Double separateTax = 0d; //单独计税税额






    /**
     * 两种计税模式
     * 1、合并计税：
     * 2、单独计税
     * 2.1、工资税：
     * 2.2、奖金税：
     */
    @Test
    public void test01(){

        printSalary(19000,11000,0.05,40000, 1500);
        printSalary(12000,0,0.12,0, 0);

//        Double salary = 1000000d; //工资
//        Double bonus = 100000d;   //奖金
//        // 合并计税
//        mergeTax = getMergeTax(salary,bonus);
//        // 单独计税
//        separateTax = getSeparateTax(salary,bonus);
//        System.out.println("合并计税总额：" + mergeTax);
//        System.out.println("单独计税总额：" + separateTax);
    }

    /**
     *
     * @param basicBase 基本工资
     * @param meritBase 绩效工资
     * @param ratio 公积金缴纳比例
     * @param yearEndBonus 年终奖
     */
    public void printSalary(Integer basicBase, Integer meritBase, Double ratio, Integer yearEndBonus, Integer rent){
        Double totalTaxIncome = 0D; // 累计纳税收入
        Double totalTax = 0D; // 累计纳税
        Double specialDeduction = basicBase * 0.175;
        Double allMy = 0D;
        for (int i = 1; i <= 12; i++) {
            System.out.print(i + "月：");
            Double currentTaxIncome = basicBase + meritBase - specialDeduction;
            if (currentTaxIncome > 5000){
                currentTaxIncome -= 5000;
            }
            totalTaxIncome += currentTaxIncome;
            Double currentReserveFund = basicBase * ratio * 2;
            System.out.print(" 公积金：" + currentReserveFund);
            Double currentTax = getTaxPayable(totalTaxIncome, totalTax);
            totalTax += currentTax;
            System.out.print(" 缴税：" + currentTax);
            Double my = (basicBase + meritBase - specialDeduction - currentTax + currentReserveFund - rent);
            allMy += my;
            System.out.println(" 到手+公积金：" + my);
        }
        System.out.println("合计：" + allMy + yearEndBonus);
    }

    public Double getTaxPayable(Double totalTaxIncome, Double totalTax){
        Double currentTax = 0D;
        if (totalTaxIncome > 960000){
            currentTax = 250080 + (totalTaxIncome - 960000) * 0.45 - totalTax;
        }else if (totalTaxIncome > 660000){
            currentTax = 145080 + (totalTaxIncome - 660000) * 0.35 - totalTax;
        }else if (totalTaxIncome > 420000){
            currentTax = 73080 + (totalTaxIncome - 420000) * 0.3 - totalTax;
        }else if (totalTaxIncome > 300000){
            currentTax = 43080 + (totalTaxIncome - 300000) * 0.25 - totalTax;
        }else if (totalTaxIncome > 144000){
            currentTax = 11880 + (totalTaxIncome - 144000) * 0.2 - totalTax;
        }else if (totalTaxIncome > 36000){
            currentTax = 1080 + (totalTaxIncome - 36000) * 0.1 - totalTax;
        }else {
            currentTax = totalTaxIncome * 0.03 - totalTax;
        }
        return currentTax;
    }



    /**
     * 计算单独计税总税
     * @param salary
     * @param bonus
     * @return
     */
    public Double getSeparateTax(Double salary,Double bonus){
        Double yearSalaryTax = getMergeTax(salary,0d);//月薪税
        Double bonusPerMonth = bonus / 12; //月均奖金
        if (bonusPerMonth <= 3000){// 0.03
            return bonus * 0.03 + yearSalaryTax;
        }else if (bonusPerMonth <= 12000){// 0.1   210
            return bonus * 0.1 + yearSalaryTax - 210 * 12;
        }else if (bonusPerMonth <= 25000){// 0.2   1410
            return bonus * 0.2 + yearSalaryTax - 1410 * 12;
        }else if (bonusPerMonth <= 35000){// 0.25  2660
            return bonus * 0.25 + yearSalaryTax - 2660 * 12;
        }else if (bonusPerMonth <= 55000){// 0.3   4410
            return bonus * 0.3 + yearSalaryTax - 4410 * 12;
        }else if (bonusPerMonth <= 80000){// 0.35  7160
            return bonus * 0.35 + yearSalaryTax - 7160 * 12;
        }else { // 0.45   15160
            return bonus * 0.45 + yearSalaryTax - 15160 * 12;
        }
    }

    /**
     * 计算合并计税总税
     * @param salary
     * @param bonus
     * @return
     */
    public Double getMergeTax(Double salary,Double bonus){
        // 1、计算总年薪
        Double totalSalary = salary + bonus;
        // 2、计算应缴税额
        Double taxDue = totalSalary - (rentDeduction + insurancesFund + startPoint) * 12;
        if (taxDue <= 36000){ // 0.03
            return taxDue * 0.03;
        }else if (taxDue <= 144000){ // 0.10
            return taxDue * 0.1 - 2520;
        }else if (taxDue <= 300000){ // 0.20
            return taxDue * 0.2 - 16920;
        }else if (taxDue <= 420000){ // 0.25
            return taxDue * 0.25 - 31920;
        }else if (taxDue <= 660000){ // 0.30
            return taxDue * 0.3 - 52920;
        }else if (taxDue <= 960000){ // 0.35
            return taxDue * 0.35 - 85920;
        }else { // 0.45
            return taxDue * 0.45 - 181920;
        }
    }

}
